package cn.org.donald.service.impl;

import cn.org.donald.framework.util.StringUtil;
import cn.org.donald.mapper.LogOperatorMapper;
import cn.org.donald.pojo.dto.system.QueryLogOperatorDto;
import cn.org.donald.pojo.entity.system.LogOperator;
import cn.org.donald.service.system.LogOperatorService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Donald
 * @since 2020-12-29
 */
@Service(version = "1.0.0", group = "donald")
public class LogOperatorServiceImpl extends ServiceImpl<LogOperatorMapper, LogOperator> implements LogOperatorService {

    @Autowired
    private LogOperatorMapper logOperatorMapper;

    @Override
    public boolean add(LogOperator logOperator) {
        return logOperatorMapper.insert(logOperator)>0;
    }

    @Override
    public IPage<LogOperator> selectPage(QueryLogOperatorDto queryLogOperatorDto) {
        // 精确查询
        String application = queryLogOperatorDto.getApplication();
        Integer code = queryLogOperatorDto.getCode();
        String operatorName = queryLogOperatorDto.getOperatorName();
        Date queryDateEnd = queryLogOperatorDto.getQueryDateEnd();
        Date queryDateStart = queryLogOperatorDto.getQueryDateStart();
        Long timeConsumeEnd = queryLogOperatorDto.getTimeConsumeEnd();
        Long timeConsumeStart = queryLogOperatorDto.getTimeConsumeStart();
        // 模糊查询
        String path = queryLogOperatorDto.getPath();
        String requestType = queryLogOperatorDto.getRequestType();
        String title = queryLogOperatorDto.getTitle();

        QueryWrapper<LogOperator> wrapper = new QueryWrapper<>();
        if (StringUtil.isNotEmpty(application)) {
            wrapper.eq("application",application);
        }
        if (StringUtil.isNotEmpty(operatorName)) {
            wrapper.eq("operator_name",operatorName);
        }
        if (StringUtil.isNotEmpty(code)) {
            wrapper.eq("code",code);
        }
        if (StringUtil.isNotEmpty(requestType)) {
            wrapper.eq("requestType",requestType);
        }
        if (queryDateEnd != null) {
            wrapper.lt("gmt_created",queryDateEnd);
        }
        if (queryDateStart != null) {
            wrapper.gt("gmt_created",queryDateStart);
        }
        if (timeConsumeEnd != null) {
            wrapper.lt("gmt_created",timeConsumeEnd);
        }
        if (timeConsumeStart != null) {
            wrapper.gt("gmt_created",timeConsumeStart);
        }
        if (StringUtil.isNotEmpty(path)) {
            wrapper.like("path",path);
        }
        if (StringUtil.isNotEmpty(title)) {
            wrapper.like("title",title);
        }
        return logOperatorMapper.selectPage(queryLogOperatorDto, wrapper);
    }
}
